'use strict';

const Controller = require('egg').Controller;

class InfomationController extends Controller {
    //获取
    async getInfomation() {
        const { ctx } = this;
        const data = await this.app.mysql.select('infomation');
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //搜索
    async searchInfomation() {
        const { ctx } = this;
        const { title, state } = ctx.query;
        const data = await this.app.mysql.query(`SELECT * FROM infomation WHERE title Like "%${title}%" 
    and state Like "%${state}%"`);
        data.forEach((item, index) => {
            item.key = index + 1;
        })
        ctx.body = {
            code: 200,
            data,
        };
    }

    //删除
    async deleteInfomation() {
        const { ctx } = this;
        const { id } = ctx.query;
        const data = await this.app.mysql.delete('infomation', {
            id: id,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "删除失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "删除成功"
        };
    }

    //新增
    async addInfomation() {
        const { ctx } = this;
        const { arr } = ctx.request.body;
        const data = await this.app.mysql.insert('infomation', {
            title: arr.title,
            originator: arr.originator,
            date: arr.date,
            content: arr.content,
            key: 0,
            state: 1,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "新增失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "新增成功"
        };
    }


    //编辑
    async editorInfomation() {
        const { ctx } = this;
        const { arr } = ctx.request.body;
        const list = await this.app.mysql.get('infomation', { id: arr.id });
        const data = await this.app.mysql.update('infomation', {
            title: arr.title,
            originator: arr.originator,
            date: arr.date,
            content: arr.content,
            key: list.key,
            state: list.state,
            id: list.id,
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "修改失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "修改成功"
        };
    }

    //开关的编辑
    async stateInfomation() {
        const { ctx } = this;
        const { id } = ctx.request.body;
        let list = await this.app.mysql.get('infomation', { id: id });
        list.state = !list.state;
        const data = await this.app.mysql.update('infomation', {
            ...list
        });
        if (data.affectedRows !== 1) {
            ctx.body = {
                code: 500,
                message: "修改失败"
            };
        }
        ctx.body = {
            code: 200,
            message: "修改成功"
        };
    }
}

module.exports = InfomationController;
